using System;
using System.Collections.Generic;
using System.Linq;
using TLDRML.Tokens;

namespace TLDRML.Nodes
{
    public class ValueNode : Node
    {
        public string Value { get; set; }

        public ValueNode()
        {
        }

        public ValueNode(string value)
        {
            Value = value;
        }

        public override IEnumerable<Token> Parse(IEnumerable<Token> tokens)
        {
            var token = tokens.First();

            if (token is LiteralToken)
                Value = ((LiteralToken) token).Value;
            else if (token is StringToken)
                Value = ((StringToken)token).Value;
            else
            {
                throw new InvalidOperationException("Unexpected token");
            }

            return tokens.Skip(1);
        }

        public bool Equals(ValueNode other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Value, Value);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (ValueNode)) return false;
            return Equals((ValueNode) obj);
        }

        public override int GetHashCode()
        {
            return (Value != null ? Value.GetHashCode() : 0);
        }
    }
}